package
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import mx.containers.Canvas;

	public class MineBoard extends Canvas
	{		
		public function MineBoard() {
			super();
			_VerticalCount = 5;
			_HorizontalCount = 5;
			addEventListener(MouseEvent.CLICK, mouseClick);
			
		}
	
		public function setup(xSize: Number, ySize: Number): void {
			_HorizontalCount = xSize;
			_VerticalCount = ySize;
			width = xSize * CELL_SIZE;
			height = ySize * CELL_SIZE;
			var i: int;
			var j: int;
			var column: Array;
			var b: MineFieldItemButton;
			if (_Items == null || xSize != _Items.length || (HorizontalCount > 0 && _Items[0].length != ySize)) {
					if (_Items != null) {
	 				for each(var x: Array in _Items) {
						for each(var y: MineFieldItemButton in x) {
							y.removeEventListener(MineFieldEvent.REFRESH_ICON, onRefreshIcon);
							removeChild(y);
						}
					}
				}
				_Items = new Array(HorizontalCount);
				for (i = 0; i < _HorizontalCount; i++) {
					column = new Array(_VerticalCount);
					_Items.push(column);
					for(j = 0; j < _VerticalCount; j++) {
						b = new MineFieldItemButton();
						
						b.x = CELL_SIZE * i;
						b.y = CELL_SIZE * j;
						//b.setup(board[i][j] as Number);
						b.setup(0);
						b.addEventListener(MineFieldEvent.REFRESH_ICON, onRefreshIcon);
												
						this.addChild(b);					
					}
				}
			}
			else
			{
				for (i = 0; i < _HorizontalCount; i++) {
					column = _Items[i];
					for(j = 0; j < _VerticalCount; j++) {
						b = column[j];
						b.setup(0);
					}
				}	
			}
/* 			else 
			
				for(i = 0; i < xSize; i++) {
					column = _Items[i] as Array;
					for	(j = 0; j < ySize; j++) {
						b = column[j] as MineFieldItemButton;
						b.setup(board[i][j] as Number);
					}
				}
			} */
		}
		
		public function loadFromTransferArray(board: ByteArray): void {
			if (board.length != HorizontalCount * VerticalCount)
				throw new Error("Invalid array size");
		}
			
		public function get HorizontalCount(): Number {
			return _HorizontalCount;	
		}

		public function get VerticalCount(): Number {
			return _VerticalCount;
		}
		
		public function get MineCount(): Number {
			return _MineCount;
		}
			
		override protected function createChildren():void {
			super.createChildren();
		}	
		
		private function isInBound(x: Number, y: Number): Boolean {
			return x >= 0 && x < _HorizontalCount && y >= 0 && y < _VerticalCount;
		}
		
		private function forEachArround(x: Number, y: Number, proc: Function): void {
			for (var i: Number = -1; i <= 1; ++i) {
				for (var j: Number = -1; j <= 1; ++j) {
					if ((i != 0 || j != 0) && isInBound(x + i, y + j)) {
						proc(_Items[x + i][y + j]);
					}
				}				
			}
		}
		
		private function onRefreshIcon(event: MineFieldEvent): void {
			var b: MineFieldItemButton = event.target as MineFieldItemButton;
			if (b.isOpened)
			{
				b.data = BoardIcons.FromIndex(b.Value);
			}
			else
			{
				b.data = BoardIcons.CLOSED;
			}
		}
		
		private function mouseClick(event: MouseEvent): void {
			var r: Point = globalToLocal(new Point(event.stageX, event.stageY));
			var x: Number = r.x;
			var y: Number = r.y;
			var xInd: Number = x / CELL_SIZE;
			var yInd: Number = y / CELL_SIZE;
			var b: MineFieldItemButton = event.target as MineFieldItemButton;
			if (!b.isOpened) {
				dispatchEvent(new CellOperationEvent(xInd, yInd, 1));
			}
		}
		
/* 		override public function dispatchEvent(event:Event):Boolean
		{
			if (event is GameStartedEvent)
			{
				var i: Number = 0;
			}
			return super.dispatchEvent(event);
		} */
		
		private var _HorizontalCount: Number;
		private var _VerticalCount: Number;
		private var _Items: Array;
		private var _MineCount: Number;
		
		private static const CELL_SIZE: Number = 16;		
	}
}